import { KlMcClient } from "./KlMcClient";
const assert = require("assert");

const ipAddr = "127.0.0.1"; // <PLC IPADDRESS>
const port = "6000"; // <PLC PORT>

const mcProtocolClient = new KlMcClient(ipAddr, port); // initialize client.

const testCase1 = async () => {
  for (let i = 0; i < 1000; ++i) {
    const strToSend = `hello world ${i}`;
    await mcProtocolClient.writeString("D0", strToSend);
    const str = await mcProtocolClient.readString("D0", strToSend.length);
    assert.strictEqual(strToSend, str, "字符串不相等");
    console.log("#######1", str);

    const arrToSend = [1, 2, 3, 4, i];
    await mcProtocolClient.setWords("D1", arrToSend);
    const vals = await mcProtocolClient.getWords("D1", 5);
    assert.deepStrictEqual(vals, arrToSend, "数组不相等");
    console.log("#######2", vals);

    await mcProtocolClient.setWord("D2", i);
    const val = await mcProtocolClient.getWord("D2");
    assert.strictEqual(val, i, "字符串不相等");
    console.log("#######3", val);
  }
};

const sampleFunc = async () => {
  await mcProtocolClient.open(); // connection to plc.
  await testCase1();
  mcProtocolClient.close(); // close client.
};

(async () => {
  await sampleFunc();
})();
